package com.neuedu.myweather;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * 自定义Mapper类
 *
 * @author Dell
 */

public class WeatherMapper extends Mapper<LongWritable, Text, WeatherWritable, Text> {

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text,
            WeatherWritable, Text>.Context context) throws IOException, InterruptedException {
        String line = value.toString();
        if (StringUtils.isBlank(line)) {
            return;
        }
        String[] items = line.split("\t");
        if (items.length != 2) {
            return;
        }
        String year = items[0].substring(0, 4);
        String temperature = items[1].substring(0, items[1].length() - 1);
        WeatherWritable w = new WeatherWritable(Integer.parseInt(year), Double.parseDouble(temperature));
        context.write(w, value);
    }
}
